***** REPLICATION FILE FOR BRAZYS AND KOTSADAM 2020 Sunshine or Curse? Foreign Direct Investment, the OECD Anti-Bribery Convention, and Individual Corruption Experiences in Africa ****

***Set working directory
**** SET YOUR WORKING DIRECTORY HERE"
cd "G:\My Drive\Aid_FDI_GEOREFERENCE\Data\ISQ_Replication"
**************************************************************

log using ISQ_Replication

**** SET YOUR WORKING DIRECTORY HERE"
global data="G:\My Drive\Aid_FDI_GEOREFERENCE\Data\ISQ_Replication"
**************************************************************

global radius = 100
global precision = 2

set more off
set maxvar 20000
set matsize 11000

use "$data\ISQ_REPLICATION.dta", replace

**** Set Results File HERE
global results="C:\Results\ISQ_Replicate"

*** RUN Respondent Regressions at 50km base

**TABLE 1

*Model 1 - All FDI
gen active=active_binary_50
gen inactive=inactive_50
reg bribe_d active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_T1.doc, append dec(3) ctitle(All FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 2 - FDI from High Corruption Countries
gen active50_highcorr=0
replace active50_highcorr=active_binary_50 if mean_cpi_50<=median_cpi
gen inactive50_highcorr=0
replace inactive50_highcorr=inactive_50 if mean_cpi_50<=median_cpi
gen active=active50_highcorr
gen inactive=inactive50_highcorr
reg bribe_d active inactive cd* yd*  age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_T1.doc, append dec(3) ctitle(High Corr FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 3 - FDI from Low Corruption Counties
gen active50_lowcorr=0
replace active50_lowcorr=active_binary_50 if mean_cpi_50>median_cpi
gen inactive50_lowcorr=0
replace inactive50_lowcorr=inactive_50 if mean_cpi_50>median_cpi
gen active=active50_lowcorr
gen inactive=inactive50_lowcorr
reg bribe_d active inactive cd* yd*  age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_T1.doc, append dec(3) ctitle(Low Corr FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 4 - FDI from High Proportion of ABC Signatory countries
gen active50_highwgb=0
replace active50_highwgb=active_binary_50 if mean_wgb_50>mean_wgb_source
gen inactive50_highwgb=0
replace inactive50_highwgb=inactive_50 if mean_wgb_50>mean_wgb_source
gen active=active50_highwgb
gen inactive=inactive50_highwgb
reg bribe_d active inactive cd* yd*  age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_T1.doc, append dec(3) ctitle(High ABC) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 5 - FDI from Low Proportion of ABC Signatory countries
gen active50_lowwgb=0
replace active50_lowwgb=active_binary_50 if mean_wgb_50<=mean_wgb_source
gen inactive50_lowwgb=0
replace inactive50_lowwgb=inactive_50 if mean_wgb_50<=mean_wgb_source
gen active=active50_lowwgb
gen inactive=inactive50_lowwgb
reg bribe_d active inactive cd* yd*  age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_T1.doc, append dec(3) ctitle(Low ABC) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 6 - Often Without Cash
gen active=active_binary_50
gen inactive=inactive_50
reg often_without_cash active inactive cd* yd* age age2 working female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_T1.doc, append dec(3) ctitle(DV Without Cash) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 

*Model 7 - Working
reg working active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_T1.doc, append dec(3) ctitle(DV Working) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

***STEP 8 Run Model 1 Regression at all $radius for FIGURE 2

local x = 1
local i = 0
while `x' < $radius + 1 {
gen activex=active_binary_`x'
gen inactivex=inactive_`x'
eststo: quietly reg bribe_d activex inactivex cd* yd*  age age2 female urban, cl(cluster)
test activex-inactivex=0
eststo, add(p_diff r(p))
estadd scalar difference = _b[activex]-_b[inactivex]
drop activex inactivex 
local x = `x' + 1
}


esttab using "$data\ISQ_REPLICATE_figure2.csv", stat(p_diff) plain replace


*** NEED TO MANUALLY Transpose VALUES FROM  "$data\ISQ_REPLICATE_figure2.csv" and assign p-value labels in figure_2.csv to make figure in R below *******
estimates drop est*

****IN R
*install.packages("ggplot2")
*install.packages("gcookbook")
*library(ggplot2)

*Figure 2 - Diff_Diff

*** Set working directory ******
*tiff("fig2_high_res.tiff", units="in", width=6, height=6, res=300)
*mydata <- read.csv("figure_2.csv", header=TRUE)
*dist_plot <- ggplot(mydata, aes(x=km_dist, y=diff_diff, shape=p_value)) + geom_point(colour='red', size=2) + scale_shape_manual(values=c(1, 8, 3, 2, 16)) + stat_smooth(aes(group=1), method=loess, alpha=.4) + ylab("Active Inactive Diff in Diff") + xlab("Project Capture Radius in KM") + ggtitle("Figure 1: Diff in Diff at Different Spatial Capture Radii") + theme(plot.title = element_text(hjust = 0.5), legend.position="bottom")
*dist_plot
*dev.off()

***  Robustness checks

*** Table A1 Robustness Checks - 5 year "burn in"

*Model 1 - All FDI
gen active=active_binary_50
gen inactive=inactive_50
reg bribe_d active inactive cd* yd* age age2 female urban if year>2007, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A1.doc, append dec(3) ctitle(All FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 2 - FDI from High Corruption Countries

gen active=active50_highcorr
gen inactive=inactive50_highcorr
reg bribe_d active inactive cd* yd*  age age2 female urban if year>2007, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A1.doc, append dec(3) ctitle(High Corr FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 3 - FDI from Low Corruption Counties

gen active=active50_lowcorr
gen inactive=inactive50_lowcorr
reg bribe_d active inactive cd* yd*  age age2 female urban if year>2007, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A1.doc, append dec(3) ctitle(Low Corr FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 4 - FDI from High Proportion of ABC Signatory countries

gen active=active50_highwgb
gen inactive=inactive50_highwgb
reg bribe_d active inactive cd* yd*  age age2 female urban if year>2007, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A1.doc, append dec(3) ctitle(High ABC) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 5 - FDI from Low Proportion of ABC Signatory countries

gen active=active50_lowwgb
gen inactive=inactive50_lowwgb
reg bribe_d active inactive cd* yd*  age age2 female urban if year>2007, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A1.doc, append dec(3) ctitle(Low ABC) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 6 - Often Without Cash
gen active=active_binary_50
gen inactive=inactive_50
reg often_without_cash active inactive cd* yd* age age2 working female urban if year>2007, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A1.doc, append dec(3) ctitle(DV Without Cash) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 

*Model 7 - Working
reg working active inactive cd* yd* age age2 female urban if year>2007, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A1.doc, append dec(3) ctitle(DV Working) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*** Table A2 Robustness Checks - "Lagged" FDI

*Model 1 - All FDI L1
gen active=active_binaryl1
gen inactive=inactive_50
reg bribe_d active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A2.doc, append dec(3) ctitle(Baseline L1) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active

*Model 2 - All FDI L2
gen active=active_binaryl2
reg bribe_d active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A2.doc, append dec(3) ctitle(Baseline L2) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active

*Model 3 - Often Without Cash L1
gen active=active_binaryl1
reg often_without_cash active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A2.doc, append dec(3) ctitle(DV Without Cash L1) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active

*Model 4 - Often Without Cash L2
gen active=active_binaryl2
reg often_without_cash active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A2.doc, append dec(3) ctitle(DV Without Cash L2) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active

*Model 5 - Working L1
gen active=active_binaryl1
reg working active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A2.doc, append dec(3) ctitle(DV Working L1) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active

*Model 6 - Working L2
gen active=active_binaryl2
reg working  active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A2.doc, append dec(3) ctitle(DV Working L2) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive


*** Table A3 - Logit Estimator

*Model 1 - All FDI
gen active=active_binary_50
gen inactive=inactive_50
logit bribe_d active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A3.doc, append dec(3) ctitle(All FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 2 - FDI from High Corruption Countries

gen active=active50_highcorr
gen inactive=inactive50_highcorr
logit bribe_d active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A3.doc, append dec(3) ctitle(High Corr FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 3 - FDI from Low Corruption Counties

gen active=active50_lowcorr
gen inactive=inactive50_lowcorr
logit bribe_d active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A3.doc, append dec(3) ctitle(Low Corr FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 4 - FDI from High Proportion of ABC Signatory countries

gen active=active50_highwgb
gen inactive=inactive50_highwgb
logit bribe_d active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A3.doc, append dec(3) ctitle(High ABC FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 5 - FDI from Low Proportion of ABC Signatory countries

gen active=active50_lowwgb
gen inactive=inactive50_lowwgb
logit bribe_d active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A3.doc, append dec(3) ctitle(Low ABC) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 6 - Often Without Cash
gen active=active_binary_50
gen inactive=inactive_50
logit often_without_cash active inactive cd* yd* age age2 working female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A3.doc, append dec(3) ctitle(DV Without Cash) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 

*Model 7 - Working
logit working active inactive cd* yd* age age2 female urban, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A3.doc, append dec(3) ctitle(DV Working) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive


*** Table A4 - Multi-level mixed effects

*Model 1 - All FDI
gen active=active_binary_50
gen inactive=inactive_50
mixed bribe_d active inactive cd* yd* age age2 female urban ||countrycode: || cluster:, emonly
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A4.doc, append dec(3) ctitle(All FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 2 - FDI from High Corruption Countries
gen active=active50_highcorr
gen inactive=inactive50_highcorr
mixed bribe_d active inactive cd* yd* age age2 female urban ||countrycode: || cluster:, emonly
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A4.doc, append dec(3) ctitle(High Corr FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 3 - FDI from Low Corruption Countries
gen active=active50_lowcorr
gen inactive=inactive50_lowcorr
mixed bribe_d active inactive cd* yd* age age2 female urban ||countrycode: || cluster:, emonly
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A4.doc, append dec(3) ctitle(Low Corr FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 4 - FDI from High Proportion of ABC Signatory countries

gen active=active50_highwgb
gen inactive=inactive50_highwgb
mixed bribe_d active inactive cd* yd* age age2 female urban ||countrycode: || cluster:, emonly
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A4.doc, append dec(3) ctitle(High ABC FDI) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 5 - FDI from Low Proportion of ABC Signatory countries

gen active=active50_lowwgb
gen inactive=inactive50_lowwgb
mixed bribe_d active inactive cd* yd* age age2 female urban ||countrycode: || cluster:, emonly
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A4.doc, append dec(3) ctitle(Low ABC) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 6 - Often Without Cash
gen active=active_binary_50
gen inactive=inactive_50
mixed often_without_cash active inactive cd* yd* age age2 female urban ||countrycode: || cluster:, emonly
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A4.doc, append dec(3) ctitle(DV Without Cash) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 

*Model 7 - Working
mixed working active inactive cd* yd* age age2 female urban ||countrycode: || cluster:, emonly
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A4.doc, append dec(3) ctitle(DV Working) keep(active inactive) title(`v') nocons adds(Difference in difference, e(difference), Chi2 test: active-inactive=0, r(chi2), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive


*Model 8 - Conley SEs
*xi: ols_spatial_HAC bribe_d active_binary_50 inactive_50 i.country i.year age age2 female urban, timevar(year) panelvar(cluster) lat(latitude) lon(longitude) distcutoff(500) lagcutoff(2)
*test active_binary_50-inactive_50=0
*estadd scalar difference = _b[active_binary_50]-_b[inactive_50]
*outreg2 using $results/ISQRR_FDI_A4.doc, ctitle(Conley SEs) keep(active_binary_50 inactive_50)  slow(100) adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)')addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 


**TABLE A6 - Alternative Controls

*Model 1 - All FDI
gen active=active_binary_50
gen inactive=inactive_50
xi: reg bribe_d active inactive ln_gdp gdp_growth polity2 age age2 female urban working often_without_cash i.psector yd*, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A6.doc, append dec(3) ctitle(All FDI) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 2 - FDI from High Corruption Countries
gen active=active50_highcorr
gen inactive=inactive50_highcorr
xi: reg bribe_d active inactive ln_gdp gdp_growth polity2 age age2 female urban working often_without_cash i.psector yd*, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A6.doc, append dec(3) ctitle(High Corr FDI) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 3 - FDI from Low Corruption Counties
gen active=active50_lowcorr
gen inactive=inactive50_lowcorr
xi: reg bribe_d active inactive ln_gdp gdp_growth polity2 age age2 female urban working often_without_cash i.psector yd*, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A6.doc, append dec(3) ctitle(Low Corr FDI) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 4 - FDI from High Proportion of ABC Signatory countries
gen active=active50_highwgb
gen inactive=inactive50_highwgb
xi: reg bribe_d active inactive ln_gdp gdp_growth polity2 age age2 female urban working often_without_cash i.psector yd*, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A6.doc, append dec(3) ctitle(High ABC) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 5 - FDI from Low Proportion of ABC Signatory countries
gen active=active50_lowwgb
gen inactive=inactive50_lowwgb
xi: reg bribe_d active inactive ln_gdp gdp_growth polity2 age age2 female urban working often_without_cash i.psector yd*, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A6.doc, append dec(3) ctitle(Low ABC) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*Model 6 - Often Without Cash
gen active=active_binary_50
gen inactive=inactive_50
xi: reg often_without_cash active inactive ln_gdp gdp_growth polity2 age age2 female urban working i.psector yd*, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A6.doc, append dec(3) ctitle(DV Without Cash) title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 

*Model 7 - Working
xi: reg working active inactive ln_gdp gdp_growth polity2 age age2 female urban often_without_cash i.psector yd*, cl(cluster)
test active-inactive=0
estadd scalar difference = _b[active]-_b[inactive]
outreg2 using $results/ISQ_REPLICATE_A6.doc, append dec(3) ctitle(DV Working)  title(`v') nocons adds(Difference in difference, e(difference), F test: active-inactive=0, r(F), p value, `r(p)') adec(3)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Country FE, YES) 
drop active inactive

*** Collapse by cluster to use cluster quasi-panel to test cluster FE and cluster wealth diff-in-diff

collapse (mean) bribe_d active_count* inactive* active_binary* active_amount* mean_cpi* mean_wgb* g_lat g_lon age age2 urban rural femal male education meeting raisedissue demonstrated discusspolitics working member_religious member_union member_business member_development member_other corruption_president_office corruption_elected_leaders corruption_government_officials corruption_police corruption_border_officials corruption_judges corruption_local_businessmen corruption_foreign_businessmen corruption_teachers corruption_religious_leaders corruption_NGO_leaders bribe_permit bribe_school bribe_service bribe_border bribe_police bribe_other police_station police often_without_cash voted radio_news careful elections_free corruption_MP local_corruption corruption_loc_gov_officials corruption_tax_officials bribe_medical_lastyear bribe_elections_lastyear adults interested_public corruption_tr_lead bribe_water_lastyear ethnic police_roadblock countrycode latitude longitude place_id ln_investment (first) country ,by(cluster year)

egen xx=group(country year)
quietly tab xx, gen(cyd)
drop xx

egen xx=group(year)
quietly tab xx, gen(yd)
drop xx

egen xx=group(country)
quietly tab xx, gen(cd)
drop xx

tsset cluster year

*** Run Model 1 Regression at all $radius for cluster using site-level FEs to create FIGURE 3
local x = 1
local i = 0
while `x' < $radius + 1 {
gen activex=active_binary_`x'
gen inactivex=inactive_`x'
eststo: quietly xtreg bribe_d activex  cd* yd*  age age2 female urban, fe cl(cluster)
drop activex inactivex 
local x = `x' + 1
}

esttab using "$data\ISQ_REPLICATE_figure3.csv", b(5) se(5) plain replace
estimates drop est*

****IN R

*** Need to manually transpose and calculate CIs from "$data\ISQ_REPLICATE_figure3.csv" to generate file figure3.csv
*tiff("fig3_high_res.tiff", units="in", width=6.5, height=6, res=300)
*mydata <- read.csv("figure3.csv", header=TRUE)
*distance_cluster <- ggplot(mydata, aes(x=Capture_Distance, y=Beta_Active)) + geom_point(colour='red', size=2, shape=8) + geom_errorbar(aes(ymin=lci, ymax=uci), colour='red', width=.1) + stat_smooth(method=loess, alpha=.4) + ylab("Regression Coefficient on Active") + xlab("Capture Radii KM") + ggtitle("Figure 2: Active at Different Capture Radii, Cluster Quasi-Panel") + theme(plot.title = element_text(hjust = 0.5))+ scale_x_continuous(breaks=seq(0, 100, 10))
*distance_cluster
*dev.off()

**Table A5 - Panel Results

	*Model 1 - Binary 50km
	gen active=active_binary_50
	xtreg bribe_d active yd*  age age2 female urban, fe cl(cluster)
	outreg2 using $results/ISQ_REPLICATE_A5.doc, ctitle(Binary DV) keep(active)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Site FE, YES) 
	drop active
	
	*Model 2 - Count 50km
	gen ln_active_count_50=ln(active_count_50+1)
	xtreg bribe_d ln_active_count_50 yd*  age age2 female urban, fe cl(cluster)
	outreg2 using $results/ISQ_REPLICATE_A5.doc, ctitle(Count Active) keep(ln_active_count_50)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Site FE, YES) 

	*Model 3 - Amount 50km
	gen ln_active_amount_50=ln(active_amount_50+1)
	xtreg bribe_d ln_active_amount_50 yd*  age age2 female urban, fe cl(cluster)
	outreg2 using $results/ISQ_REPLICATE_A5.doc, ctitle(Amount Active) keep(ln_active_amount_50)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Site FE, YES) 

	*Model 4 - Count 50km low and high coor
	gen ln_active_count_lc_50=ln(active_count_lc_50+1)
	gen ln_active_count_hc_50=ln(active_count_hc_50+1)
	xtreg bribe_d ln_active_count_hc_50 ln_active_count_lc_50 yd*  age age2 female urban, fe cl(cluster)
	outreg2 using $results/ISQ_REPLICATE_A5.doc, ctitle(Count Active Corruption) keep(ln_active*)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Site FE, YES) 

	*Model 5 - Count 50km Signature Counts
	gen ln_active_count_sig_50=ln(active_count_sig_50+1)
	gen ln_active_count_nsig_50=ln(active_count_nsig_50+1)
	xtreg bribe_d ln_active_count_sig_50 ln_active_count_nsig_50 yd*  age age2 female urban, fe cl(cluster)
	outreg2 using $results/ISQ_REPLICATE_A5.doc, ctitle(Count Active WGB) keep(ln_active*)  slow(100) addtext(Baseline controls, YES, Year FE, YES, Site FE, YES) 

***** FIGURE 1 IN R	

*setwd("G:/My Drive/Aid_FDI_GEOREFERENCE/Data/ISQ_Replication")

*install.packages(c("rworldmap", "rworldxtra", "RColorBrewer",
    "maptools", "classInt"))

*library('rworldmap')
*library('rworldxtra')
*library('RColorBrewer')
*library('maptools')
*library('classInt')

*worldmap <- getMap(resolution = "high")
*dim(worldmap)

*par(mar=c(0,0,0,0))     # Set 0 margins
*africa_base <- worldmap[which(worldmap$REGION=="Africa"),]               
*plot(africa_base, col="white", bg="lightblue", xlim = c(-10, 50), ylim = c(-40, 40), asp = 1)


*#Create Base World Map
*world_map <- map_data("world")
*p <- ggplot() + coord_fixed() +
*xlab("") + ylab("")

*base_africa_messy <- p + geom_polygon(data=world_map, aes(x=long, y=lat, group=group), colour = "black", fill = "black") + xlim(-20,60) + ylim(-40,40) 


*cleanup <-
*theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
*panel.background = element_rect(fill = 'lavender', colour = 'lavender'),
*axis.line = element_line(colour = "black"), legend.position="none",
*axis.ticks=element_blank(), axis.text.x=element_blank(),
*axis.text.y=element_blank())
*base_africa <- base_africa_messy + cleanup
*base_africa

*FDI_locations <- read.csv("FDI_locations.csv", header= TRUE )



*#FDI projects
*africa_fdi <- base_africa + geom_point(data=FDI_locations, aes(x=longitude, y=latitude, size=inv_cpi), colour="white", fill="white", pch=15, alpha=(.75))

*Cluster_locations <- read.csv("Cluster_locations.csv", header= TRUE )

*#with respondent clusters
*tiff("fig1_a_high_res.tiff", units="in", width=6.5, height=6, res=300)
*africa_clusters <- africa_fdi + geom_point(data=Cluster_locations, aes(x=g_lon, y=g_lat), colour="purple", fill="purple", pch=8, size=1, alpha=(0.15))
*africa_clusters
*dev.off()

*#FDI projects by WGB_Signatory
*tiff("fig1_b_high_res.tiff", units="in", width=6.5, height=6, res=300)
*africa_fdi_wgb <- base_africa + geom_point(data=FDI_locations, aes(x=longitude, y=latitude, colour=wgb_code, fill=wgb_code, size=inv_cpi), pch=18, alpha=(1)) + scale_colour_gradient2(low="green", high="red") + scale_fill_gradient2(low="red", high="yellow")
*africa_fdi_wgb
*dev.off()


	
	
**** FIGURE 4 IN R

*library(ggmap)
**** NOTE - NEED ACTIVE GOOGLE API KEY ****
*API_KEY ="YOUR API KEY HERE"
*register_google(key=API_KEY)


*mapImageData <- get_map(location = c(lon = 28.777621, lat = -13.02701), color = "color", source = "google", maptype = "roadmap", zoom = 10)

*p <-ggmap(mapImageData)

*factory <- read.csv("Factory.csv", header= TRUE)

*plot_factory <- p + geom_point(data=factory, aes(x=lon, y=lat), pch=15, size=4, colour= "black", alpha=(1))

*afro <- read.csv("Dangote.csv", header= TRUE)

*tiff("fig4_1_high_res.tiff", units="in", width=5, height=5, res=300)
*plot_bribe <- plot_factory + geom_point(data=afro, aes(x=lon_shim, y=lat_shim, colour=post, pch=bribe), size=2, alpha=(1))
*plot_bribe
*dev.off()

*tiff("fig4_3_high_res.tiff", units="in", width=5, height=5, res=300)
*plot_cash <- plot_factory + geom_point(data=afro, aes(x=lon_shim, y=lat_shim, colour=post, pch=without_cash), size=2, alpha=(1))
*plot_bribe
*dev.off()

*tiff("fig4_2_high_res.tiff", units="in", width=5, height=5, res=300)
*plot_work <- plot_factory + geom_point(data=afro, aes(x=lon_shim, y=lat_shim, colour=post, pch=work), size=2, alpha=(1))
*plot_work
*dev.off()
